import pymysql

db_config = {
    'host': '118.24.27.37',
    'port': 3307,
    'user': 'longtao',
    'password': 'LongTao%1314',
    'database': 'hf',
    'charset': 'utf8'
}
sql = 'select * from hf_area'
# 建立连接
conn = pymysql.connect(**db_config)
# 获取游标
cursor = conn.cursor()
cursor.execute(sql)
# 查询
# result = cursor.fetchall()
# for i in result:
#     print(i)

## 增删改有事务 执行sql 需commit
# 增加
# sql_insert = 'insert into hf_cn_area (code,name,pcode) values ("1","hehe","12")'
# cursor.execute(sql_insert)
# conn.commit()
# 删除
sql_delete = 'delete from hf_cn_area where id = 2 '
cursor.execute(sql_delete)
conn.commit()
# 修改
# sql_update = 'update hf_cn_area set code = 10001 where id = 2'
# cursor.execute(sql_update)
# conn.commit()

# 关闭连接
cursor.close()
conn.close()
# conn = pymysql.connect(**db_config)
# # 上下文管理器
# with conn.cursor() as cur:
#     cur.execute(sql)
#     print(cur.fetchall())
#
# conn.close()
